package controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.EndItemDAO;
import dao.GetItemDAO;
import databean.ItemBean;

public class GetItemAction extends Action{

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) {
		
		//call from api.js
		if (request.getParameter("id")!=null) {
			String item_id=request.getParameter("id");
			
			ItemBean rs = GetItemDAO.getItem(item_id);
			
			if (rs==null) {
				request.setAttribute("error", GetItemDAO.getMessage());
				return "getItem.view";
			} else {
				request.setAttribute("rq", item_id);
				request.setAttribute("rs", rs);
			}
			return "getItemResult.view";
		}
		
		
		
		String add=request.getParameter("add");
		
		//case the first time come
		if(add==null||add.length()==0){
			return "getItem.view";
		}
		
		//get user input information
		String itemid=request.getParameter("givenkeyword");		
		
		//update to ebay
		if(itemid!=null){
			
			ItemBean rs = GetItemDAO.getItem(itemid);
			
			if (rs==null) {
				request.setAttribute("error", GetItemDAO.getMessage());
				return "getItem.view";
			} else {
				request.setAttribute("rq", itemid);
				request.setAttribute("rs", rs);
			}

		}
		return "getItemResult.view";
	}

}
